﻿using BlogApi.Data;
using BlogApi.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BlogApi.Services
{
    public class FanRepository:IFanRepository
    {
        private readonly BlogDbContext _context;

        public FanRepository(BlogDbContext context)
        {
            _context = context ?? throw new ArgumentNullException(nameof(context));
        }
        public async Task<IEnumerable<Fan>> GetFansAsync(Guid authorId)
        {
            if (authorId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(authorId));
            }
            return await _context.Fans_table.Where(m => m.AuthorId == authorId).ToListAsync();
        }
        public async Task<IEnumerable<Fan>> GetFansByPageAsync(Guid authorId, int pageSize, int pageIndex)
        {
            if (authorId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(authorId));
            }
            return await _context.Fans_table.Where(m => m.AuthorId == authorId).Skip(pageSize * pageIndex).Take(pageSize).ToListAsync();
        }
        public async Task<IEnumerable<Fan>> GetUserFocusByPageAsync(Guid userId, int pageSize, int pageIndex)
        {
            if (userId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(userId));
            }
            return await _context.Fans_table.Where(m => m.UserId == userId).Skip(pageSize * pageIndex).Take(pageSize).ToListAsync();
        }
        public async Task<Fan> GetFanAsync(Guid id)
        {
            if (id == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(id));
            }
            return await _context.Fans_table.FirstOrDefaultAsync(fan => fan.Id == id);
        }
        public async Task<Fan> GetFanAsync(Guid authorId, Guid userId)
        {
            return await _context.Fans_table.FirstOrDefaultAsync(fan => fan.AuthorId == authorId && fan.UserId == userId);
        }
        public void AddFan(Fan fan)
        {
            if (fan == null)
            {
                throw new ArgumentNullException(nameof(fan));
            }
            _context.Fans_table.Add(fan);
        }
        public void UpdateFan(Fan fan)
        {
            //由于数据库上下文会对实体进行跟踪 所以这句话可以不写
            //_context.Entry(fan).State = EntityState.Modified;
        }
        public void DeleteFan(Fan fan)
        {
            if (fan == null)
            {
                throw new ArgumentNullException(nameof(fan));
            }
            _context.Fans_table.Remove(fan);
        }
        public async Task<bool> FanExistsAsync(Guid id)
        {
            if (id == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(id));
            }
            return await _context.Fans_table.AnyAsync(fan => fan.Id == id);
        }
        public async Task<bool> AuthorUserExistsAsync(Guid authorId, Guid userId)
        {
            return await _context.Fans_table.AnyAsync(fan => fan.AuthorId == authorId && fan.UserId == userId);
        }
        public async Task<bool> SaveAsync()
        {
            return await _context.SaveChangesAsync() >= 0;
        }
    }
}
